package com.hw.one.collect.dust.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.collect.client.HttpClient;
import com.hw.one.collect.dust.constans.SjwConstants;
import com.hw.one.collect.dust.po.SiteInfo;
import com.hw.one.collect.dust.redis.BaseRedis;
import com.hw.one.collect.mapper.SiteDao;
import com.hw.one.collect.util.HttpClientUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/17 14:37.
 *
 * 市建委数据拉取任务
 */
@Component
public class SjwSiteDataJob extends RaiseDustBaseJob{

    private static Logger log = Logger.getLogger(SjwSiteDataJob.class);


    @Resource
    private SiteDao siteDao;


    public void start() {
        SjwConstants.getRegionIdWithName();
        Map<String, String> regionIdWithOtherCode = SjwConstants.regionIdWithOtherCode;
        Map<String, String> regionIdWithName = SjwConstants.regionIdWithName;
        Map<String, String> regionIdWithCode = SjwConstants.regionIdWithCode;
        int updateCount = 0;
        int addCount = 0;
        for (String key : regionIdWithOtherCode.keySet()) {
            String url = SjwConstants.Base_URI+key;
            log.debug("-------------------当前请求的城市：" + regionIdWithName.get(key) + "-----------");
            Map<String, String> resultMap = HttpClientUtil.sendGetWithParams(url, new HashMap<String, String>(), new HashMap<String, String>());
            if ("200".equals(resultMap.get("statusCode"))) {
                log.debug("----------------请求成功------------------");
                String resultJson = resultMap.get("result");
                JSONObject json = JSONObject.parseObject(resultJson);
                JSONArray dataList = JSONArray.parseArray(json.getString("data"));
                log.debug("--------------------请求到的数据----------------------------");
                log.debug(dataList.toString());

                for (Object data : dataList) {
                    JSONObject dataObj = JSONObject.parseObject(data.toString());
                    String address = dataObj.getString("bsiteLoc"); // 地址
                    String linkMan = dataObj.getString("bsiteMgr"); // 联系人
                    String siteName = dataObj.getString("bsiteName"); // 站点名称
                    String siteNo = dataObj.getString("bsiteNo"); // 站点代码
                    String siteProject = dataObj.getString("bsiteProject"); // 项目名称
                    String siteStruc = dataObj.getString("bsiteStruc"); //
                    String siteType = dataObj.getString("bsiteType"); //
                    String buildUnit = dataObj.getString("buildUnit"); // 建筑公司
                    String buildId = dataObj.getString("hkBuildId"); // 建筑id
                    String id = dataObj.getString("id"); // id
                    String latitude = dataObj.getString("latitude"); // 纬度
                    String linkPhone = dataObj.getString("linkphone"); // 联系电话
                    String longitude = dataObj.getString("longitude"); // 经度
                    String regionId = dataObj.getString("xzqhId"); // 行政区划id
                    String regionName = dataObj.getString("xzqhMc"); // 行政区划名称
                    String status = dataObj.getString("status"); // 状态

                    SiteInfo siteInfo = new SiteInfo();
                    siteInfo.setId(UUID.randomUUID().toString().replace("-", ""));
                    siteInfo.setSiteCode(siteNo);
                    siteInfo.setSiteName(siteName);
                    siteInfo.setAddress(address);
                    siteInfo.setContactTel(linkPhone);
                    siteInfo.setLongitude(Double.valueOf(longitude));
                    siteInfo.setLatitude(Double.valueOf(latitude));
                    siteInfo.setRegionCode(regionIdWithCode.get(key));
                    siteInfo.setSiteTypeCode("106");
                    siteInfo.setIsThied("1");

                    siteInfo.setUpdateDate(new Date());
                    siteInfo.setOrgId(regionIdWithOtherCode.get(key));
                    List<SiteInfo> siteInfoList = siteDao.selectSiteInfoBySiteCode(siteNo);
                    // 未查到数据
//                    baseRedis.addList(siteName + "siteCode", siteNo);
                    baseInfoService.insertSiteCodeBySiteNameFromRedis(siteName, siteNo);
                    if (null == siteInfoList || siteInfoList.size() < 1) {
                        log.info("--------------数据库没有数据，插入----------");
                        siteInfo.setCreateDate(new Date());
                        siteInfo.setIsUse("0");
                        siteDao.insertSiteInfo(siteInfo);
                        baseInfoService.insertRegionCodeWithSiteCode(siteNo, regionIdWithOtherCode.get(key));
                        addCount ++;
                    }else {
                        log.info("--------------数据库有数据，更新-----");
                        siteDao.updateSiteBySiteCode(siteInfo);
                        updateCount ++;
                    }
                }
            }
        }
        log.info("本次更新工地信息：" + updateCount + "个 ;新增信息：" + addCount + "个；");
    }
}
